package com.mzm;


import java.util.Collection;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

public class PathUtils {

	public static String PATH_SEPERATOR = "/";
	public static String ROOT_NODE_NAME = "";

	public static int getNestingLevel(String path) {
		return StringUtils.countMatches(path, PATH_SEPERATOR);
	}

	/**
	 * 
	 * @param path
	 * @param path2
	 * @return <code>true</code> if path2 is subpath (child) of path1.
	 */
	public static boolean isSubpathOf(String path1, String path2) {
		if (path2 == null) {
			return false;
		}
		return path2.indexOf(path1) == 0 && path2.length() > path1.length();
	}

	/**
	 * 
	 * @param path1
	 * @param path2
	 * @return <code>true</code> if path2 is subpath of path1 or equal.
	 */
	public static boolean isSubpathOrEqual(String path1, String path2) {
		if (path2 == null) {
			return false;
		}
		if (path1.equals(path2))
			return true;
		return path2.indexOf(path1) == 0;
	}

	public static String getParentPath(String path) {
		// String[] pathElements = StringUtils.split(path, '/');
		int ind = path.lastIndexOf('/');
		if (ind == 0 && path.length() > 1) {
			return "/";
		}

		if (ind <= 0) {
			return null;
		}
		return path.substring(0, ind);
	}

	public static String getLeafNodeName(String path) {
		// String[] pathElements = StringUtils.split(path, '/');
		int ind = path.lastIndexOf('/');
		if (ind == 0 && path.length() == 1) {
			return ROOT_NODE_NAME;
		}

		return path.substring(ind + 1);
	}

	/**
	 * Parses the given path into the elements it is composed of.
	 * 
	 * @param path
	 * @return
	 */
	public static String[] parsePathElements(String path) {
		String[] splits = StringUtils.split(path, PATH_SEPERATOR);
		String[] elements = new String[splits.length + 1];
		for (int i = 0; i < splits.length; i++) {
			elements[i + 1] = splits[i];
		}
		elements[0] = ROOT_NODE_NAME;
		return elements;
	}

	/**
	 * Creates a single String path representaion from the given elements.
	 * 
	 * @param elements
	 * @return
	 */
	public static String buildPathFromElements(String[] elements) {
		if (!ROOT_NODE_NAME.equals(elements[0])) {
			throw new IllegalStateException("Path must start with root element");
		}
		return StringUtils.join(elements, PATH_SEPERATOR);
	}

	public static String buildSubPathFromElements(String[] elements, int endIndex) {
		if (!ROOT_NODE_NAME.equals(elements[0])) {
			throw new IllegalStateException("Path must start with root element");
		}
		String[] sub = (String[]) ArrayUtils.subarray(elements, 0, endIndex + 1);
		String path = StringUtils.join(sub, PATH_SEPERATOR);
		return path.isEmpty() ? PATH_SEPERATOR : path;
	}

	public static boolean isRoot(String path) {
		return PATH_SEPERATOR.equals(path);
	}

	public static boolean isAnyContainedInPath(Collection<String> paths, String path){
		for (String p : paths) {
			if(isSubpathOrEqual(path, p)){
				return true;
			}
		}
		return false;
	}
}

